const db = uniCloud.database()
module.exports = {
	add: async ({
		owner_name,
		plate_number,
		driver_number,
		goods,
		expected_departure_time,
		estimated_time_of_arrival,
		vehicle_model,
		code,
		state
	}) => {
		let enterDB = db.collection('cc_enter')

		const res = await enterDB.add({
			owner_name,
			plate_number,
			driver_number,
			vehicle_model,
			goods,
			expected_departure_time,
			estimated_time_of_arrival,
			created_time: Date.now(),
			update_time: Date.now(),
			code,
			state
		})

		return {
			code: 200,
			msg: '创建成功'
		}
	},
	edit: async ({
		_id,
		owner_name,
		plate_number,
		driver_number,
		goods,
		expected_departure_time,
		estimated_time_of_arrival,
		vehicle_model,

	}) => {
		let enterDB = db.collection('cc_enter')

		const res = await enterDB.where({
			_id
		}).update({
			owner_name,
			plate_number,
			driver_number,
			goods,
			expected_departure_time,
			estimated_time_of_arrival,
			vehicle_model,
			updated_time: Date.now(),
			created_time: Date.now(),
		})
		return {
			code: 200,
			msg: '编辑成功'
		}
	},
	changeState: async ({
		state,
		_id,
		isFallBack
	}) => {
		let enterDB = db.collection('cc_enter')
		const dbItem = await enterDB.where({
			_id
		})

		if (state === '已到达') {
			if (isFallBack) {
				await dbItem.update({
					state,
					updated_time: Date.now(),
				})
			} else {
				await dbItem.update({
					state,
					updated_time: Date.now(),
					appoint_time: Date.now(),
					appoint_status: '已预约',

				})
			}
		} else if (state === '卸车完成') {
			await dbItem.update({
				state,
				updated_time: Date.now(),
				appoint_status: '已完成'
			})
		} else if (state === '运输中') {
			await dbItem.update({
				state,
				updated_time: Date.now(),
				appoint_status: '已取消'
			})
		} else {
			await dbItem.update({
				state,
				updated_time: Date.now(),
			})
		}
		const {
			data: pending_list = []
		} = await enterDB.where({
			appoint_status: '已预约'
		}).orderBy(
			'appoint_time', 'asc'
		).get()
	
		const list = pending_list.map((item, index) => {
			return enterDB.where({
				_id: item._id
			}).update({
				appoint_index: index + 1
			})
		})
		await Promise.all(list)
		return {
			code: 200,
			msg: '状态更新成功'
		}
	},
	list: async ({
		code
	}) => {
		let enterDB = db.collection('cc_enter')

		if (code) {
			enterDB = await enterDB.where({
				code
			})
		}
		const dbCmd = db.command
		const res = await enterDB.where({
			is_delete: dbCmd.neq('1')
		}).orderBy('created_time', 'desc').get();
		return res;
	},
	detail: async (_id) => {
		let enterDB = db.collection('cc_enter')

		if (_id) {
			enterDB = enterDB.where({
				_id
			})
		}
		const res = await enterDB.get({
			getOne: true,
		})
		return res;
	},
	delete: async (_id) => {
		let enterDB = db.collection('cc_enter')

		if (_id) {
			enterDB = enterDB.where({
				_id
			}).update({
				is_delete: '1'
			})
		}

		return {
			code: 200,
			msg: '删除成功'
		};
	},
	pendingNum: async () => {
		let enterDB = db.collection('cc_enter')

		enterDB = enterDB.where({
			appoint_status: '已预约'
		})
		const res = await enterDB.count()
		return res;
	}
}